package taskman;
/*
 * COMP 354 Project - Team 6
 */

import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileInputStream;

public class FileInput {
	private String id;
	private String title;
	private String description;
	private double duration;
	private String deliverable;
	private String deadline;
	private String person;
	private ArrayList<Task> tasks;
	
	private static String RESOURCE_FOLDER = "resources/";

	public FileInput(ArrayList<Task> tasks) {
		// default constructor, load from default file name
		// adds reference to task list to add to it directly
		this.tasks = tasks;
		loadFromFile(RESOURCE_FOLDER + "input.txt");
	}

	public FileInput(ArrayList<Task> tasks, String filename) {
		// constructor allowing a filename as parameter
		// adds reference to task list to add to it directly
		loadFromFile(filename);
	}

	private void resetVars() {
		// resets all variables to default
		id = "";
		title = "";
		description = "";
		duration = 0;
		deliverable = "";
		deadline = "";
		person = "";
	}
	
	@Deprecated
	private void loadFromFile(String filename) {
		// reset variables
		resetVars();

		// create file reader object and read from file
		Scanner f = null;
		try {
			f = new Scanner(new FileInputStream(filename));
		} catch (FileNotFoundException e) {
			// if couldn't read from file, exit program
			System.out.println("Couldn't find the input file \"" + filename
					+ "\"! Terminating program.\n");
			System.exit(0);
		}

		// create String object to read the file line by line
		String readStr = null;
		// create String object to split the line into variable and value
		String[] readStrSplit;
		// var and val serve as pointers to readStrSplit
		String var = null, val = null;

		// loop until end of file is reached
		while (f.hasNext()) {
			// read next line
			readStr = f.nextLine();

			// split line into variable and value
			readStrSplit = readStr.split("=", 2);
			var = readStrSplit[0];

			// before setting val, make sure readStrSplit has at least 2
			// elements in it
			if (readStrSplit.length > 1)
				val = readStrSplit[1];
			else
				val = "";

			// check what variable is and determine action
			if (var.equalsIgnoreCase("id")) {
				// marks beginning of new task item
				// perform actions based on current variables values
				export();

				// reset vars to default
				resetVars();

				// set id
				id = val;
			}
			// cases below are to set the variables
			else if (var.equalsIgnoreCase("title")) {
				title = val;
			} else if (var.equalsIgnoreCase("description")) {
				description = val;
			} else if (var.equalsIgnoreCase("duration")) {
				// need to convert from String to double
				duration = Double.valueOf(val).doubleValue();
			} else if (var.equalsIgnoreCase("deliverable")) {
				deliverable = val;
			} else if (var.equalsIgnoreCase("deadline")) {
				deadline = val;
			} else if (var.equalsIgnoreCase("person")) {
				person = val;
			}
		} // end of while loop

		// file reading ended. export data
		export();

		// close file reader
		f.close();
	}

	private void export() {
		// if no id is entered, ignore this function
		if (id == "")
			return;

		/*
		* insert code here for output stuff.
		* variables are:
		* 	private String id;
		*	private String title;
		*	private String description;
		*	private double duration;
		*	private String deliverable;
		*	private String deadline;
		*	private String person;
		*
		* assume all values are present
		* 
		*/
		
		// adds new task based on last gathered info
//		tasks.add(new Task(this.id, this.title, this.description,
//				this.duration, this.deliverable, this.deadline, this.person));

		// // the following is for debugging
		// System.out.println("id=" + id);
		// System.out.println("title=" + title);
		// System.out.println("description=" + description);
		// System.out.println("duration=" + duration);
		// System.out.println("deliverable=" + deliverable);
		// System.out.println("deadline=" + deadline);
		// System.out.println("person=" + person);
		// System.out.println("****************************");
	}
}
